package jcu.ict.profaid;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteCursor;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
public class FormPhoneBook extends Activity {
	final int CONTEXT_MENU_DELETE_ITEM = 1;
	final int CONTEXT_MENU_UPDATE = 2;
	private ListView lv_course;
	private PhoneDBAdapter pdb;
	private String id_course;
	private String id_question;
	private MyAdapterPhone favContexted;
	private AdapterContextMenuInfo menuInfo;
	private String user_id;

	@SuppressLint("NewApi")
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.form_my_question);
		
		Bundle bundle = this.getIntent().getExtras();
		id_course = (String) bundle.getCharSequence("setIdCourse");
		id_question = (String) bundle.getCharSequence("setQuestionId");
		user_id = (String) bundle.getCharSequence("setUserId");

		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		ColorDrawable colorDrawable = new ColorDrawable(
				Color.parseColor("#0099CC"));
		actionBar.setBackgroundDrawable(colorDrawable);
		
		pdb = new PhoneDBAdapter(getBaseContext());
		pdb.open();
		
		lv_course = (ListView)findViewById(R.id.lstMyQuestion);
		
		ArrayList<Object> listCourse = PopulateList(null);
		favContexted = new MyAdapterPhone(this, R.layout.phonebook, R.id.txtUp, listCourse);
		lv_course.setAdapter(favContexted);
		
		registerForContextMenu(lv_course);
		
		pdb.close();
	}
	
	
	private ArrayList<Object> PopulateList(String abc) {
		// TODO Auto-generated method stub
		SQLiteCursor krsr = null;
		try{
			krsr = (SQLiteCursor) pdb.populateNumber(id_course,id_question);
				
		}catch(Exception ex){
			Log.e("salah",ex.toString());
			return null;
		}
		
		ArrayList<Object> queryResult = new ArrayList<Object>();
		final int colCnt = krsr.getColumnCount();
		
		while (krsr.moveToNext()) {
            ArrayList<Object> satuRow = new ArrayList<Object>();
            for (int i = 0; i < colCnt; i++) {
                /**
                 * In android 2.2 isNull is not exclusive so we need elseif's
                 */
                if (krsr.isNull(i)) {
                    satuRow.add("");
                } else if (krsr.isString(i)) {
                    satuRow.add(krsr.getString(i));
                } else if (krsr.isLong(i)) { // integer
                    satuRow.add(krsr.getLong(i));
                } else if (krsr.isFloat(i)) { // real
                    satuRow.add(krsr.getDouble(i));
                } else if (krsr.isBlob(i)) {
                    satuRow.add(krsr.getBlob(i));
                }
            }
            queryResult.add(satuRow);
        }
        krsr.close();
		return queryResult;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// app icon in action bar clicked; go home
			Intent intent = new Intent(FormPhoneBook.this, MainResult.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			Bundle bundle = new Bundle();
			bundle.putString("setInfoCourse", id_course);
			bundle.putString("setQuestionId", id_question);
			bundle.putCharSequence("setUserId", user_id);
			intent.putExtras(bundle);
			startActivity(intent);
			finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenu.ContextMenuInfo menuInfo) {

		menu.add(Menu.NONE, CONTEXT_MENU_DELETE_ITEM, Menu.NONE, "Call");
		menu.add(Menu.NONE, CONTEXT_MENU_UPDATE, Menu.NONE, "Message");
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			Intent in = new Intent(FormPhoneBook.this,
					MainResult.class);
			Bundle bundle = new Bundle();
			bundle.putString("setInfoCourse", id_course);
			bundle.putString("setQuestionId", id_question);
			bundle.putCharSequence("setUserId", user_id);
			in.putExtras(bundle);
			startActivity(in);
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}

	public boolean onContextItemSelected(MenuItem item) {
		menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();

		ListAdapter adapter = lv_course.getAdapter();
		Object obj = adapter.getItem(menuInfo.position);
		String x = obj.toString();

		switch (item.getItemId()) {
		case CONTEXT_MENU_DELETE_ITEM:
			Intent callIntent = new Intent(Intent.ACTION_CALL);
			callIntent.setData(Uri.parse("tel:" + x));
			startActivity(callIntent);
			return (true);
		case CONTEXT_MENU_UPDATE:
			Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
			smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
			smsIntent.setType("vnd.android-dir/mms-sms");
			smsIntent.setData(Uri.parse("sms:" + x));
			startActivity(smsIntent);
			return (true);
		}
		return (super.onOptionsItemSelected(item));
	}
}

